﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using DMS.Business.Utility;
using DMS.Business.WebControls;
using DMS.Business.Modules.Organization;

namespace DMS.Web.Common.Controls
{
    public partial class RoleTree : System.Web.UI.UserControl
    {
        #region 属性参数
        private object eventLocker = new object();
        private string _RootNodeText = "根节点";

        public string RootNodeText
        {
            get { return _RootNodeText; }
            set { _RootNodeText = value; }
        }

        private string _NavigateUrlTemplate = string.Empty;
        /// <summary>
        /// 节点跳转路径模板
        /// </summary>
        public string NavigateUrlTemplate
        {
            get
            {
                return this._NavigateUrlTemplate;
            }
            set
            {
                this._NavigateUrlTemplate = value;
            }
        }
        /// <summary>
        /// 节点点击目标
        /// </summary>
        public string Target
        {
            get
            {
                return this.tvRoles.Target;
            }
            set
            {
                this.tvRoles.Target = value;
            }
        }

        /// <summary>
        /// 是否显示节点复选框
        /// </summary>
        public TreeNodeTypes ShowCheckBoxes
        {
            get
            {
                return this.tvRoles.ShowCheckBoxes;
            }
            set
            {
                this.tvRoles.ShowCheckBoxes = value;
            }
        }
        public event TreeNodeEventHandler SelectedChanged
        {
            add
            {
                lock (eventLocker)
                {
                    this.tvRoles.TreeNodeCheckChanged += value;
                }
            }
            remove
            {
                lock (eventLocker)
                {
                    this.tvRoles.TreeNodeCheckChanged -= value;
                }
            }
        }
        public event EventHandler NodeClick
        {
            add
            {
                lock (eventLocker)
                {
                    this.tvRoles.SelectedNodeChanged += value;
                }
            }
            remove
            {
                lock (eventLocker)
                {
                    this.tvRoles.SelectedNodeChanged -= value;
                }
            }
        }
        public TreeNode SelectedItem
        {
            get
            {
                return this.tvRoles.SelectedNode;
            }
        }
        public TreeNodeCollection SelectedItems
        {
            get
            {
                return this.tvRoles.CheckedNodes;
            }
        }
        #endregion

        #region 控件事件
        /// <summary>
        /// 控件加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.InitTree();
            }
        }
        #endregion

        #region 内部方法
        /// <summary>
        /// 初始化部门树
        /// </summary>
        public void InitTree()
        {
            this.tvRoles.CheckedNodes.Clear();
            TreeNode rootNode = this.CreateNode(null);
            this.tvRoles.Nodes.Add(rootNode);

            int total = 0;
            string condition = string.Empty;

            if (!string.IsNullOrEmpty(Request["RoleType"]))
            {
                string typeStr = string.Empty;
                string[] types = this.Request["RoleType"].ToString().Split(",".ToCharArray());
                foreach (string type in types)
                {
                    if (Enum.IsDefined(typeof(RoleTypes), type))
                    {
                        RoleTypes roleType = (RoleTypes)Enum.Parse(typeof(RoleTypes), type);
                        typeStr += string.Format("{0},", (int)roleType);
                    }
                }

                if (!string.IsNullOrEmpty(typeStr))
                {
                    condition += string.Format(" AND [RoleType] IN ({0})", typeStr.Trim(",".ToCharArray()));
                }
            }
            List<Role> roles = UserHelper.GetRoles(-1, 1, out total, condition, null);
            if (SystemContext.Current.LoginAuthUser.IsVersatilePerson)
            {
                roles = (from r in roles
                         where r.Name == "物资申请员" || r.Name == "部门限额可见"
                         select r).ToList();
            }
            foreach (Role role in roles)
            {
                TreeNode node = this.CreateNode(role);
                rootNode.ChildNodes.Add(node);
            }
        }

        /// <summary>
        /// 创建节点
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        private TreeNode CreateNode(Role role)
        {
            TreeNodePlus node = this.tvRoles.CreateNodePlus();
            node.SelectAction = TreeNodeSelectAction.Select;
            //node.PopulateOnDemand = true;

            if (role != null)
            {
                node.Text = role.Name;
                node.Value = role.ID.ToString();
                node.ToolTip = role.Name;
                node.NavigateUrl = this.CreateNodeNavigateUrl(role);
            }
            else
            {
                node.Text = this._RootNodeText;
                node.Value = string.Empty;
            }
            return node;
        }

        /// <summary>
        /// 构成节点导航路径 
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        private string CreateNodeNavigateUrl(Role role)
        {
            string url = this._NavigateUrlTemplate;

            if (this._NavigateUrlTemplate.Contains("{ID}"))
            {
                url = url.Replace("{ID}", role.ID.ToString());
            }
            if (this._NavigateUrlTemplate.Contains("{Name}"))
            {
                url = url.Replace("{Name}", role.Name);
            }
            if (this._NavigateUrlTemplate.Contains("{Type}"))
            {
                url = url.Replace("{Type}", role.Type.ToString());
            }

            return url;
        }
        #endregion
    }
}